package com.iwuke.dance.util;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.TextView;

import com.iwuke.dance.R;

import androidx.annotation.NonNull;

/**
 * @author: 魏从响
 * @date: 2025/9/15
 */
public class TimeHandlerUtil {
    private final Context context;
    private final TextView tv_time;
    private long s = 59;
    private Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    if (s>10){
                        s--;
                        tv_time.setText(s+"");
                    }else if (s>0){
                        s--;
                        tv_time.setTextColor(context.getColor(R.color.color_f0));
                        tv_time.setText("0"+s);
                    }
                    if (handler!=null)
                        handler.sendEmptyMessageDelayed(1,1000);
                    break;
            }
        }
    };

//    private int getColor(int colorF0) {
//        return 0;
//    }

    public TimeHandlerUtil(Context context, TextView tv_time, long time){
        this.context = context;
        this.tv_time = tv_time;
        s = time;
    }

    public void sendEmptyMessageDelayed(){
        if (handler!=null){
            handler.sendEmptyMessageDelayed(1,1000);
        }
    }

    public void removeCallbacks(){
        if (handler!=null){
            handler.removeCallbacks(null);
            handler=null;
        }
    }

}
